﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AusProject.OrderComponents;

namespace AusProject
{
    public partial class FormComponentTypeDesigner : Form
    {
        public FormComponentTypeDesigner()
        {
            InitializeComponent();
            FormComponentType_Resize(null, null);
            cmbColEdit.SelectedIndex = 0;
        }

        private void FormComponentType_Resize(object sender, EventArgs e)
        {
            ocDemo.Left = ocDemo.Width < panelComponent.Width ? (panelComponent.Width - ocDemo.Width) / 2 : 0;
            ocDemo.Top = (panelComponent.Height - ocDemo.Height) / 2;
            panelProperties.Left = (this.Width - panelProperties.Width) / 2;
        }

        private ComponentColumnInfo[] columns = new ComponentColumnInfo[] { new ComponentColumnInfo() };

        private ComponentColumnInfo[] tmpColumns;


        private void numCols_ValueChanged(object sender, EventArgs e)
        {
            int nCols = (int)numCols.Value;

            cmbColEdit.Items.Clear();

            tmpColumns = new ComponentColumnInfo[nCols];

            for (int i = 0; i < nCols; i++)
            {
                if (i < columns.Length)
                {
                    tmpColumns[i] = columns[i];
                }
                else
                {
                    tmpColumns[i] = new ComponentColumnInfo();
                }
                cmbColEdit.Items.Add("# " + i);
            }

            columns = tmpColumns;
            ocDemo.Columns = columns;

            cmbColEdit.SelectedIndex = 0;
        }

        private void cmbColEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbColEdit.SelectedIndex >= 0)
            {
                txtTitle.Text = columns[cmbColEdit.SelectedIndex].Title;
                chkShowBorder.Checked = columns[cmbColEdit.SelectedIndex].ShowTitle;
                txtWidthPercent.Text = columns[cmbColEdit.SelectedIndex].WidthPercent.ToString();
                grpCellProperties.Enabled = true;

                for (int i = 0; i < numCols.Value; i++)
                {
                    ocDemo.SetTextOfCell(0, i, "");
                }
                ocDemo.SetTextOfCell(0, cmbColEdit.SelectedIndex, "Editing");
            }
            else
            {
                grpCellProperties.Enabled = false;
            }
        }

        private void txtTitle_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTitle.Text))
            {
                chkShowBorder.Checked = true;
            }
            columns[cmbColEdit.SelectedIndex].Title = txtTitle.Text;
            ocDemo.Columns = columns;
        }

        private void chkShowBorder_CheckedChanged(object sender, EventArgs e)
        {
            columns[cmbColEdit.SelectedIndex].ShowTitle = chkShowBorder.Checked;
            ocDemo.Columns = columns;
        }

        private void txtWidthPercent_Leave(object sender, EventArgs e)
        {
            double v;
            if (double.TryParse(txtWidthPercent.Text, out v))
            {
                columns[cmbColEdit.SelectedIndex].WidthPercent = v;
            }
            else
            {
                txtWidthPercent.Text = columns[cmbColEdit.SelectedIndex].WidthPercent.ToString();
            }
            ocDemo.Columns = columns;
        }

        private void txtSectionWidth_Leave(object sender, EventArgs e)
        {
            int v;
            if (int.TryParse(txtSectionWidth.Text, out v))
            {
                ocDemo.Width = v;
                FormComponentType_Resize(null, null);
            }
            else
            {
                txtSectionWidth.Text = v.ToString();
            }
        }

        private void btnNextColumn_Click(object sender, EventArgs e)
        {
            cmbColEdit.SelectedIndex = (cmbColEdit.SelectedIndex + 1) % (int)numCols.Value;
            txtTitle.Focus();
        }

        private void txtTitle_Enter(object sender, EventArgs e)
        {
            txtTitle.SelectAll();
        }

        private void txtWidthPercent_Enter(object sender, EventArgs e)
        {
            txtWidthPercent.SelectAll();
        }

        private void numCols_Enter(object sender, EventArgs e)
        {
            numCols.Select(0, 10);
        }

        private void txtSectionTitle_Enter(object sender, EventArgs e)
        {
            txtSectionTitle.SelectAll();
        }

        private void txtSectionTitle_Leave(object sender, EventArgs e)
        {
            ocDemo.Title = txtSectionTitle.Text;
        }

        public void ImportComponent(OrderComponentInfo ocInfo)
        {
            if (ocInfo != null)
            {
                this.panelComponent.Controls.Remove(ocDemo);
                ocDemo = new OrderComponent();
                ocDemo.ImportInfo(ocInfo);
                ocDemo.BackColor = SystemColors.Control;
                this.panelComponent.Controls.Add(ocDemo);
                FormComponentType_Resize(null, null);

                columns = ocDemo.Columns;

                txtSectionTitle.Text = ocDemo.Title;
                txtSectionWidth.Text = ocDemo.Width.ToString();
                numCols.Value = ocDemo.Columns.Length;
                cmbColEdit.Items.Clear();
                for (int i = 0; i < ocDemo.Columns.Length; i++)
                {
                    cmbColEdit.Items.Add("# " + i);
                }
                cmbColEdit.SelectedIndex = 0;
            }
        }

        public OrderComponentInfo GetComponentInfo()
        {
            return ocDemo.ExportInfo();
        }

        private bool accepted = false;

        public bool Accepted
        {
            get { return accepted; }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            accepted = true;
            this.Close();
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            accepted = false;
            this.Close();
        }
    }
}
